home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / jcool01.zip / TEST_VAL.C < prev    next >
C/C++ Source or Header  |  1992-08-20  |  3KB  |  120 lines

  1. //
  2. // Copyright (C) 1992 General Electric Company.
  3. //
  4. // Permission is granted to any individual or institution to use, copy, modify,
  5. // and distribute this software, provided that this complete copyright and
  6. // permission notice is maintained, intact, in all copies and supporting
  7. // documentation.
  8. //
  9. // General Electric Company provides this software "as is" without
  10. // express or implied warranty.
  11.  
  12. #include <cool/Value.h>
  13. #include <test.h>
  14. #include <iostream.h>
  15. #include <iomanip.h>
  16.  
  17. void test_value () {
  18.   TEST ("size>=8+1", sizeof(CoolValue) >=(8+1), TRUE);
  19.   CoolValue v0;
  20.   TEST ("Value v0;", TRUE, TRUE);
  21.   CoolValue v1(v0);
  22.   TEST ("Value v1(v0)", TRUE, TRUE);
  23.   TEST ("v1==v0", v1==v0, TRUE);
  24.   TEST ("v1=v0", (v1 = v0, v1==v0), TRUE);
  25.   CoolValue v2 = v0;
  26.   TEST ("Value v2=v0", v2==v0, TRUE);
  27.   TEST ("(v0='d')==(v1='d')", (v0 = 'd', v1 = 'd', v0==v1), TRUE);
  28.   TEST ("(v0='D')==(v1='d')", (v0 = 'D', v1 = 'd', v0==v1), FALSE);
  29.   CoolValue v3(v0);
  30.   TEST ("Value v3(v0)", v3==v0, TRUE);
  31.  
  32.   CoolValue i1 = 5;
  33.   TEST ("Value i1=5", i1==v3, FALSE);  
  34.   CoolValue i2(i1);
  35.   TEST ("Value i2(i1)", (i2==i1), TRUE);  
  36.   CoolValue i3;
  37.   TEST ("i3=5", (i3=5, (i3==i2 && i3==i1)), TRUE);  
  38.  
  39.   CoolValue d1 = double(-5.999999999);
  40.   TEST ("Value d1=-5.999999999", d1==v3, FALSE);  
  41.   CoolValue d2(d1);
  42.   TEST ("Value d2(d1)", (d2==d1), TRUE);  
  43.   CoolValue d3;
  44.   TEST ("d3=-5.999999999", 
  45.     (d3=double(-5.999999999), (d3==d2 && d3==d1)), TRUE);  
  46.   TEST ("d3=-6.0",
  47.     (d3=double(-6.0), (d3=-6.0, d3==d2)), FALSE);  
  48.   TEST ("int!=double", d1==i1, FALSE);
  49.   cout << d1 << "=" << &d1 << endl;
  50.   TEST ("<<", TRUE, TRUE);
  51.  
  52.   // Conversion from one type to another by promotion,
  53.   // Can loose precision when convert from int to float, or from double to float.
  54.   CoolValue c;
  55.   TEST ("unsigned(char)", unsigned(c='a')==unsigned('a'), TRUE);
  56.   TEST ("int(char)", int(c='A')==int('A'), TRUE);
  57.   CoolValue i;
  58.   TEST ("long(int)", long(i=1000)==long(1000), TRUE);
  59.   TEST ("double(int)", (i=int(-5.5), double(i)==double(-5.0) 
  60.             || double(i)==double(-6.0)), TRUE);
  61.   {
  62.     float f0 =-5.12345;                // double(float(-5.12345)) may
  63.     CoolValue f;                // be optimized by compiler.
  64.     double d1=0, d2=0;                // and different by more than 1bit.
  65.     TEST ("double(float)", (d1 = double(f=f0), d2 = double(f0), d1==d2), TRUE);
  66.     double d0 = -5.123456789123456789;
  67.     CoolValue d;
  68.     float f1=0, f2=0;
  69.     TEST ("float(double)", (f1 = float(d=d0), f2 = float(d0), f1==f2), TRUE);
  70.   }
  71. }
  72.  
  73. void test_conversion () {
  74.   char c = 'z', cc = 0;
  75.   CoolValue C(c);
  76.   cc = C;
  77.   TEST ("char", cc==c, TRUE);
  78.   int i = 15, ii = 0;
  79.   CoolValue I(i);
  80.   ii = I;
  81.   TEST ("int", ii==i, TRUE);
  82.   unsigned u = 10, uu = 0;
  83.   CoolValue U(u);
  84.   uu = U;
  85.   TEST ("unsigned", uu==u, TRUE);
  86.   long l = 20000000, ll = 0;
  87.   CoolValue L(l);
  88.   ll = L;
  89.   TEST ("long", ll==l, TRUE);
  90.   float f = 12.3, ff = 0;
  91.   CoolValue F(f);
  92.   ff = F;
  93.   TEST ("float", ff==f, TRUE);
  94.   double d = -12.3e3, dd = 0;
  95.   CoolValue D(d);
  96.   dd = D;
  97.   TEST ("double", dd==d, TRUE);
  98.   void* v = &D; void* vv = NULL;
  99.   CoolValue V(v);
  100.   vv = V;
  101.   TEST ("void*", vv==v, TRUE);
  102. }
  103.  
  104. void test_leak () {
  105.   for (;;) {
  106.     test_value();
  107.     test_conversion();
  108.   }
  109. }
  110.  
  111. int main (void) {
  112.   START("CoolValue");
  113.   test_value();
  114.   test_conversion();
  115.   //test_leak();
  116.   SUMMARY();
  117.   return 0;
  118. }
  119.  
  120.